package com.wgo.precise.client.ui.view.editor;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.ui.forms.AbstractFormPart;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.editor.FormEditor;

import com.wgo.precise.client.ui.controller.listeners.IModelChangeListener;
import com.wgo.precise.client.ui.controller.listeners.IModelUpdateListener;
import com.wgo.precise.client.ui.model.facade.IModelPersistable;
import com.wgo.precise.client.ui.model.facade.INodeParent;

/**
 * @author petterei
 *
 * @version $Id: AbstractFormPage.java,v 1.2 2006-01-30 21:52:33 petterei Exp $
 */
public abstract class AbstractPartsFormPage extends AbstractFormPage implements IModelUpdateListener, IModelChangeListener {
	
	private boolean stale = false;
	
	public AbstractPartsFormPage(FormEditor editor, String id, String title) {
		super(editor, id, title);
	}
	
	private static FillLayout createFillLayout() {
		FillLayout layout = new FillLayout(SWT.VERTICAL);
		//		FillLayout flayout = new FillLayout();//SWT.VERTICAL);
		layout.marginWidth = 8;
		layout.marginHeight = 8;
		return layout;
	}
	public static Layout createLayout() {
		return createFillLayout();
	}
	@Override
	public boolean selectReveal(Object object) {
		boolean result = super.selectReveal(object);
		getManagedForm().getForm().getBody().layout(true);
		return result;
	}
	
	public void onModelUpdate(IModelPersistable item) {
		if (getModelItem().equals(item) || null == item) {
			updateText();
		}
		if (getPartControl().isFocusControl()){
			for (IFormPart part : getManagedForm().getParts()) {
				if (part instanceof IModelUpdateListener) {
					IModelUpdateListener listener = (IModelUpdateListener) part;
					listener.onModelUpdate(item);
				}
			}
		} else {
			markStale();
		}
	}
	
	public void onModelItemDeleted(IModelPersistable item, INodeParent parent) {
		if (getPartControl().isFocusControl()){
			for (IFormPart part : getManagedForm().getParts()) {
				if (part instanceof IModelChangeListener) {
					IModelChangeListener listener = (IModelChangeListener) part;
					listener.onModelItemDeleted(item, parent);
				}
			}
		} else {
			markStale();
		}
	}
	
	public void onNewModelItemCreated(IModelPersistable item) {
		if (getPartControl().isFocusControl()){
			for (IFormPart part : getManagedForm().getParts()) {
				if (part instanceof IModelChangeListener) {
					IModelChangeListener listener = (IModelChangeListener) part;
					listener.onNewModelItemCreated(item);
				}
			}
		} else {
			markStale();
		}
	}
	
	public void onModelChange(IModelPersistable item) {
		if (getPartControl().isFocusControl()){
			for (IFormPart part : getManagedForm().getParts()) {
				if (part instanceof IModelChangeListener) {
					IModelChangeListener listener = (IModelChangeListener) part;
					listener.onModelChange(item);
				}
			}
		} else {
			markStale();
		}
//		RequirementPlugin.log(new ModelStatus(IStatus.INFO, getClass().getSimpleName() + " onModelChange dispatched"));

	}
	private void markStale() {
		stale = true;
//		for (IFormPart part : getManagedForm().getParts()) {
//			((AbstractFormPart)part).markStale();
//		}
//		RequirementPlugin.log(new ModelStatus(IStatus.INFO, getClass().getSimpleName() + " stale is set"));

	}
	
	@Override
	public synchronized void refresh() {
		if (stale) {
			for (IFormPart part : getManagedForm().getParts()) {
				if (part instanceof AbstractFormPart) {
					((AbstractFormPart)part).setFormInput(getModelItem());
				}
			}
			stale = false;
		}
	}
	/**
	 * @see org.eclipse.ui.forms.editor.FormPage#setFocus()
	 */
	@Override
	public void setFocus() {
		refresh();
		super.setFocus();
	}

	
}